package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.config.CreateConfigBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.ConfigDao;
import com.zhuss.core.db.entity.Config;
import com.zhuss.core.db.entity.ConfigType;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.StartStopEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.PingYinUtil;

/**
 * 创建配置 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/20 21:55
 */
@Component("configCreate")
public class ConfigCreate extends AbstractProcessor<Object, CreateConfigBo> implements Processor<Object, CreateConfigBo> {

    @Autowired
    private ConfigDao configDao;
    
    @Autowired
	@Qualifier("configRead")
	private ConfigRead configRead;
    
    @Autowired
	@Qualifier("configTypeRead")
    private ConfigTypeRead configTypeRead;
    
    
    /**
     * 校验
     */
    @Override
    public void check(CreateConfigBo bo) throws WpsException {
    	String type = bo.getType();
    	String value = bo.getValue();
    	Integer sort = bo.getSort();
    	Integer area = bo.getArea();
    	if (type == null || "".equals(type.trim())) {
    		throw new WpsException(ExceptionCode.配置type为空);
    	}
    	if (value == null || "".equals(value.trim())) {
    		throw new WpsException(ExceptionCode.配置value为空);
    	}
    	if (sort == null || sort < 0) {
    		// throw new WpsException(ExceptionCode.配置sort为空);
    	}
    	
    	List<ConfigType> typeList = configTypeRead.list(type, area);
    	if (typeList.isEmpty()) {
    		throw new WpsException(ExceptionCode.配置类型type不存在);
    	}
    	
    	
    	List<Config> list = configRead.list(type, value, area);
    	if (!list.isEmpty()) {
    		throw new WpsException(ExceptionCode.配置value已存在);
    	}
    	
    	List<Config> sortList = configRead.listBySort(type, sort, area);
    	Integer maxSort = configRead.getMaxSort(type, area);
    	if (!sortList.isEmpty()) {
    		throw new WpsException(ExceptionCode.配置顺序已存在, String.valueOf(maxSort));
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(CreateConfigBo param) {
    	Config record = new Config();
    	record.setType(param.getType());
    	record.setConfigKey(configRead.findMaxKeyIncr(param.getType(), param.getArea()));
    	record.setValue(param.getValue());
    	record.setTag(PingYinUtil.getPingYin(param.getValue()));
    	record.setData(param.getData());
    	record.setRemoveIdList(param.getRemoveIdList());
    	record.setStatus(param.getStatus() == null ? StartStopEnum.启用.key() : param.getStatus());
    	record.setSort(param.getSort());
    	record.setArea(param.getArea());
    	record.setParent(param.getParent());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	configDao.create(record);
    	param.setTargetId(record.getConfigId());
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreateConfigBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.配置创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getType() + ":" + param.getValue());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
